package com.dds.web.controller.app;

import java.math.BigDecimal;
import java.util.List;

import com.dds.common.utils.poi.ExcelUtil;
import com.dds.system.domain.app.AppUser;
import com.dds.system.domain.app.AppUserScoreDetail;
import com.dds.system.service.app.IAppUserScoreDetailService;
import com.dds.system.service.app.IAppUserScoreInfoService;
import com.dds.system.service.app.IAppUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dds.common.annotation.Log;
import com.dds.common.core.controller.BaseController;
import com.dds.common.core.domain.AjaxResult;
import com.dds.common.enums.BusinessType;
import com.dds.common.core.page.TableDataInfo;

/**
 * 会员Controller
 *
 * @author gaolianjie
 * @date 2021-04-01
 */
@RestController
@RequestMapping("/app/user")
public class AppUserController extends BaseController {
    @Autowired
    private IAppUserService appUserService;

    @Autowired
    private IAppUserScoreDetailService appUserScoreDetailService;

    @Autowired
    private IAppUserScoreInfoService appUserScoreInfoService;

    /**
     * 查询会员列表
     */
    @PreAuthorize("@ss.hasPermi('app:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppUser appUser) {
        startPage();
        List<AppUser> list = appUserService.selectAppUserList(appUser);
        return getDataTable(list);
    }

    /**
     * 导出会员列表
     */
    @PreAuthorize("@ss.hasPermi('app:user:export')")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AppUser appUser) {
        List<AppUser> list = appUserService.selectAppUserList(appUser);
        ExcelUtil<AppUser> util = new ExcelUtil<AppUser>(AppUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取会员详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") String userId) {
        return AjaxResult.success(appUserService.selectAppUserById(userId));
    }

    @GetMapping(value = "/scoreInfo")
    public TableDataInfo scoreInfo(AppUserScoreDetail appUserScoreDetail) {
        startPage();
        List<AppUserScoreDetail> list = appUserScoreDetailService.selectAppUserScoreDetailList(appUserScoreDetail);

        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('app:user:changeScore')")
    @PostMapping(value = "/changeScore")
    public AjaxResult changeScore(
            @RequestParam("userId") String userId,
            @RequestParam("score") BigDecimal score,
            @RequestParam(value = "remark", required = false) String remark,
            @RequestParam("type") String type
    ) {

        appUserScoreInfoService.changeScore(userId, score, remark, type);

        return toAjax(true);
    }
}
